<template>
  <a-card :bordered="false">
    <div>
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      </div>
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">

          <a-form-item label="城市服务商" style="margin-right: 10px" >
            <serveSelect @selectClick="selectClick" :value="queryParam.agencyCode"></serveSelect>
          </a-form-item>
          <a-form-item label="状态" style="margin-right: 10px">
            <a-select v-model="queryParam.status" style="width: 120px" placeholder="请选择" @change="searchQuery">
              <a-select-option value="0"> 待领取</a-select-option>
              <a-select-option value="1"> 已领取</a-select-option>
              <a-select-option value="2"> 使用中</a-select-option>
              <a-select-option value="3"> 已过期</a-select-option>
              <a-select-option value="4"> 已用完</a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="授权" style="margin-right: 10px">
            <a-select v-model="queryParam.isAuth" style="width: 120px" placeholder="请选择" @change="searchQuery">
              <a-select-option value="0"> 待授权</a-select-option>
              <a-select-option value="1"> 已授权</a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item label="用户ID" style="margin-right: 10px">
            <a-input placeholder="请输入用户ID" v-model="queryParam.userId" @pressEnter="searchQuery"></a-input>
          </a-form-item>
          <a-form-item label="广告商名称" style="margin-right: 10px">
            <a-input placeholder="请输入广告商名称" v-model="queryParam.sign" @pressEnter="searchQuery"></a-input>
          </a-form-item>
          <a-form-item label="手机号" style="margin-right: 10px">
            <a-input placeholder="请输入赠送手机号" v-model="queryParam.userPhone" @pressEnter="searchQuery"></a-input>
          </a-form-item>
          <a-form-item label="会员卡金额" style="margin-right: 10px">
            <a-input placeholder="金额" v-model="queryParam.activityBalance" @pressEnter="searchQuery"></a-input>
          </a-form-item>
          <a-form-item label="开始卡号" style="margin-right: 10px">
            <a-input placeholder="请输入开始卡号" v-model="queryParam.startCardNo" @pressEnter="searchQuery"></a-input>
          </a-form-item>
          <a-form-item label="结束卡号" style="margin-right: 10px">
            <a-input placeholder="请输入结束卡号" v-model="queryParam.endCardNo" @pressEnter="searchQuery"></a-input>
          </a-form-item>
          <div>
            <a-form-item label="领取开始时间" style="margin-right: 10px">
              <a-date-picker
                v-model="queryParam.startTime"
                :valueFormat="'YYYY-MM-DD'"
                style="width: 120px"
                @change="startChange"
              />
            </a-form-item>
            <a-form-item label="领取结束时间" style="margin-right: 10px">
              <a-date-picker
                v-model="queryParam.endTime"
                :valueFormat="'YYYY-MM-DD'"
                :disabled-date="disabledDate"
                @change="endChange"
                style="width: 120px"
              />
            </a-form-item>
          </div>
          <span style="float: left; overflow: hidden" class="table-page-search-submitButtons"> </span>
        </a-form>
      </div>
    </div>

    <div>
      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
      <a-button type="primary" @click="searchReset" icon="reload" style="margin: 0 8px">重置</a-button>
      <a-button
        style="margin-right: 10px"
        type="primary"
        @click="$envet = serverCoach()"
        v-has="'xhdjActivityBalance:SQ'"
      >
        授权</a-button
      >
      <a-upload
        v-has="'xhdjActivityBalance:imp'"
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a
        type="primary"
        icon="download"
        href=" https://121.40.239.201:41383/down/thWWXNHltsdf.xlsx"
        style="margin-left: 8px"
        >下载模板</a
      >
    </div>
    <!-- table区域-begin -->
    <!-- <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->
    <div class="ant-alert ant-alert-info">
      <span style="margin-right: 20px"
        >总共金额：<span style="font-size: 16px; color: red">{{ parseFloat(totalAmount || 0).toFixed(2) }} </span
        >元</span
      >
      <span style="margin-right: 20px"
        >已领取的金额：<span style="font-size: 16px; color: red">{{ parseFloat(getAmount || 0).toFixed(2) }} </span
        >元</span
      >
      <span style="margin-right: 20px"
        >总共张数：<span style="font-size: 16px; color: red">{{ totalNum }} </span>张</span
      >
      <span style="margin-right: 20px"
        >已领取张数：<span style="font-size: 16px; color: red">{{ getNum }} </span>张</span
      >
    </div>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      class="j-table-force-nowrap"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, getCheckboxProps: getCheckboxProps }"
      @change="handleTableChange"
    >
      <span slot="action" slot-scope="text, record" v-has="'xhdjActivityBalance:SQ'">
        <a-popconfirm
          title="确定授权吗?"
          v-has="'xhdjActivityBalance:SQ'"
          v-if="record.isAuth === 0"
          @confirm="() => handleSQ(record)"
        >
          <a>授权</a>
        </a-popconfirm>

        <!-- <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown> -->
      </span>
    </a-table>
    <xhdjActivityBalanceModal ref="modalForms" @ok="modalFormOk"></xhdjActivityBalanceModal>
    <!-- table区域-end -->
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction } from '@/api/manage'
import xhdjActivityBalanceModal from './modules/xhdjActivityBalanceModal.vue'
import serveSelect from '@/components/serveSelect.vue'

import moment from 'moment'

export default {
  name: 'xhdjFundLog',
  mixins: [JeecgListMixin],
  components: {
    xhdjActivityBalanceModal,serveSelect,
  },
  data() {
    return {
      description: '活动赠送卡列表',
      typeW: 2,
      columns: [
        {
          title: '记录编号',
          align: 'center',
          dataIndex: 'id',
        },
        {
          title: '城市服务商编号',
          align: 'center',
          dataIndex: 'agencyCode',
        },
        {
          title: '用户编号',
          align: 'center',
          dataIndex: 'userId',
          customRender: (text, record) => {
            return record.userId === 0 ? '' : record.userId
          },
        },

        {
          title: '昵称',
          align: 'center',
          dataIndex: 'nickName',
        },

        {
          title: '会员卡金额',
          align: 'center',
          dataIndex: 'activityBalance',
        },
        {
          title: '会员卡编号',
          align: 'center',
          dataIndex: 'cardNo',
        },
        {
          title: '会员卡密码',
          align: 'center',
          dataIndex: 'cardPassWord',
        },
        {
          title: '会员卡授权',
          align: 'center',
          dataIndex: 'isAuth',
          customRender: (text, record) => {
            return record.isAuth === 0 ? '未授权' : '已授权'
          },
        },
        {
          title: '广告商名称',
          align: 'center',
          dataIndex: 'sign',
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          customRender: (text, record) => {
            switch (record.status) {
              // 状态 0-未领取    1-已领取  （充值赠送默认已领取） 3已过期
              case 0:
                return '待领取'
                break
              case 1:
                return '已领取'
                break
              case 2:
                return '使用中'
                break
              case 3:
                return '已过期'
                break
              case 4:
                return '已用完'
                break
            }
          },
        },
        {
          title: '领取时间',
          align: 'center',
          dataIndex: 'gainTime',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      getAmount: 0,
      getNum: 0,
      totalAmount: 0,
      totalNum: 0,
      selectedRowKeys: [],
      url: {
        list: '/user/xhdjUserActivityBalanceRecord/list',
        importExcelUrl: '/user/xhdjUserActivityBalanceRecord/import',
        auth: '/user/xhdjUserActivityBalanceRecord/auth',
        statistics: '/user/xhdjUserActivityBalanceRecord/statistics',
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}${this.url.importExcelUrl}`
    },
  },
  created() {
    this.init()
  },
  methods: {
    startChange(value) {
      if (value) {
        this.queryParam.startTime = value + ' 00:00:00'
      } else {
        this.queryParam.startTime = value
      }
    },
    selectClick (value) {
      this.queryParam.agencyCode = value
      this.searchQuery()
    },
    endChange(value) {
      if (value) {
        this.queryParam.endTime = value + ' 23:59:59'
      } else {
        this.queryParam.endTime = value
      }
    },
    disabledDate(time) {
      return new Date(time).getTime() < new Date(this.queryParam.startTime).getTime()
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
    },
    getCheckboxProps(record) {
      return {
        props: {
          disabled: record.isAuth == 1,
        },
      }
    },
    init() {
      var params = this.getQueryParams()
      getAction(this.url.statistics, params).then((res) => {
        if (res.success) {
          let { getAmount, getNum, totalAmount, totalNum } = res.result
          this.getAmount = getAmount
          this.getNum = getNum
          this.totalAmount = totalAmount
          this.totalNum = totalNum
        }
      })
    },
    handleSQ(record) {
      this.$refs.modalForms.edit(record.id)
      this.$refs.modalForms.title = '广告商名称'
      this.$refs.modalForms.disableSubmit = false
    },

    serverCoach() {
      if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
        var ids = this.selectedRowKeys.join(',')
        this.$refs.modalForms.edit(ids)
        this.$refs.modalForms.title = '广告商名称'
        this.$refs.modalForms.disableSubmit = false
      } else {
        this.$message.error('请选择需要授权的会员卡')
      }
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';

.ant-row {
  display: flex;
}

.ant-col {
  display: flex;
}

.ant-form {
  display: flex;
}
</style>
